﻿using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.Hr.Bill;
using HengTong.Model.Extension;
using TiaoTaoHelper.Wtm.Models;


namespace HengTong.ViewModel.Hr.SysHrTransferRequestBillVms
{
    public partial class
        SysHrTransferRequestBillListVm : BaseListVm<SysHrTransferRequestBillView, SysHrTransferRequestBillSearcher>
    {
        protected override IEnumerable<IGridColumn<SysHrTransferRequestBillView>> InitGridHeader()
        {
            return new List<GridColumn<SysHrTransferRequestBillView>>
            {
                this.MakeGridHeader(x => x.PersonName),
                this.MakeGridHeader(x => x.ApplyDate),
                this.MakeGridHeader(x => x.EffectiveDate),
                this.MakeGridHeader(x => x.TransferType),
                this.MakeGridHeader(x => x.OriginalGroupName),
                this.MakeGridHeader(x => x.OriginalPositionName),
                this.MakeGridHeader(x => x.OriginalRank),
                this.MakeGridHeader(x => x.OriginalBaseSalary),
                this.MakeGridHeader(x => x.OriginalPositionSalary),
                this.MakeGridHeader(x => x.OriginalPerformanceSalary),
                this.MakeGridHeader(x => x.NewGroupName),
                this.MakeGridHeader(x => x.NewPositionName),
                this.MakeGridHeader(x => x.NewRank),
                this.MakeGridHeader(x => x.NewBaseSalary),
                this.MakeGridHeader(x => x.NewPositionSalary),
                this.MakeGridHeader(x => x.NewPerformanceSalary),
                this.MakeGridHeader(x => x.TransferReason),
                this.MakeGridHeader(x => x.Approver),
                this.MakeGridHeader(x => x.ApprovalTime),
                this.MakeGridHeader(x => x.StaffNo),
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.GroupName),
                this.MakeGridHeader(x => x.OrgName),
                this.MakeGridHeader(x => x.BillCode),
                this.MakeGridHeader(x => x.BillStatus),
                this.MakeGridHeader(x => x.WorkflowStatus),
                this.MakeGridHeader(x => x.Remark),
                this.MakeGridHeader(x => x.CanApprove).SetHide().SetFormat((a, b) =>
                {
                    if (a.CanApprove)
                    {
                        return "true";
                    }
                    else
                    {
                        return "false";
                    }
                }),

                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null)
            {
                List<SysWorkflow> ids = GetMyApproves();
                if (ids.Any())
                {
                    foreach (var item in EntityList)
                    {
                        var m = ids.Find(x => x.ModelId == item.GetID().ToString());
                        if (m != null && item.WorkflowStatus == WorkflowStatus.审核中)
                        {
                            item.CanApprove = true;
                        }
                    }
                }
            }

            EntityList.ForEach(e =>
            {
                e.CreateBy = Wtm.FormatUsername(e.CreateBy);
                e.UpdateBy = Wtm.FormatUsername(e.UpdateBy);
                e.Approver = Wtm.FormatUsername(e.Approver);
            });

            base.AfterDoSearcher();
        }


        public override IOrderedQueryable<SysHrTransferRequestBillView> GetSearchQuery()
        {
            var query = DC.Set<SysHrTransferRequestBill>()
                .CheckEqual(Searcher.PersonId, x => x.PersonId)
                .CheckBetween(Searcher.ApplyDate?.GetStartTime(), Searcher.ApplyDate?.GetEndTime(), x => x.ApplyDate,
                    includeMax: false)
                .CheckEqual(Searcher.OriginalGroupId, x => x.OriginalGroupId)
                .CheckEqual(Searcher.OriginalPositionSalary, x => x.OriginalPositionSalary)
                .CheckEqual(Searcher.NewGroupId, x => x.NewGroupId)
                .CheckEqual(Searcher.NewPositionId, x => x.NewPositionId)
                .CheckContain(Searcher.Approver, x => x.Approver)
                .CheckBetween(Searcher.ApprovalTime?.GetStartTime(), Searcher.ApprovalTime?.GetEndTime(),
                    x => x.ApprovalTime, includeMax: false)
                .CheckContain(Searcher.StaffNo, x => x.StaffNo)
                .CheckContain(Searcher.Name, x => x.Name)
                .CheckContain(Searcher.BillCode, x => x.BillCode)
                .CheckEqual(Searcher.BillStatus, x => x.BillStatus)
                .CheckEqual(Searcher.WorkflowStatus, x => x.WorkflowStatus)
                .CheckEqual(Searcher.GroupId, x => x.GroupId)
                .CheckEqual(Searcher.OrgId, x => x.OrgId)
                .Select(x => new SysHrTransferRequestBillView
                {
                    ID = x.ID,
                    PersonName = x.Person.Name,
                    ApplyDate = x.ApplyDate,
                    Name = x.Name,
                    StaffNo = x.StaffNo,
                    EffectiveDate = x.EffectiveDate,
                    TransferType = x.TransferType,
                    OriginalGroupName = x.OriginalGroup.GroupName,
                    OriginalPositionName = x.OriginalPosition.Name,
                    OriginalRank = x.OriginalRank,
                    OriginalBaseSalary = x.OriginalBaseSalary,
                    OriginalPositionSalary = x.OriginalPositionSalary,
                    OriginalPerformanceSalary = x.OriginalPerformanceSalary,
                    NewGroupName = x.NewGroup.GroupName,
                    NewPositionName = x.NewPosition.Name,
                    NewRank = x.NewRank,
                    NewBaseSalary = x.NewBaseSalary,
                    NewPositionSalary = x.NewPositionSalary,
                    NewPerformanceSalary = x.NewPerformanceSalary,
                    TransferReason = x.TransferReason,
                    Approver = x.Approver,
                    ApprovalTime = x.ApprovalTime,
                    GroupName = x.Group.GroupName,
                    OrgName = x.Org.Name,
                    BillCode = x.BillCode,
                    BillStatus = x.BillStatus,
                    WorkflowStatus = x.WorkflowStatus,
                    Remark = x.Remark,
                    CreateTime = x.CreateTime // 添加CreateTime属性
                })
                .OrderByDescending(x => x.CreateTime);
            return query;
        }
    }

    public class SysHrTransferRequestBillView : SysHrTransferRequestBill
    {
        [Display(Name = "姓名")] public String PersonName { get; set; }
        [Display(Name = "_Admin.GroupName")] public String OriginalGroupName { get; set; }
        [Display(Name = "岗位名称")] public String OriginalPositionName { get; set; }
        [Display(Name = "_Admin.GroupName")] public String NewGroupName { get; set; }
        [Display(Name = "岗位名称")] public String NewPositionName { get; set; }
        [Display(Name = "_Admin.GroupName")] public String GroupName { get; set; }
        [Display(Name = "公司名称")] public String OrgName { get; set; }
        public bool CanApprove { get; set; }
    }
}